﻿using ActionSystem.Core.ComboLogic;
using ActionSystem.Core.Ranges;
using ActionSystem.Core.Utils;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ActionSystem.Core.ComboLogic
{
    [PoolInit(typeof(GameObjectPool<CircleRangeBoxItem>),50)]
    public class CircleRangeBoxItem : RangeBoxItem2D
    {        
        protected override void PutBackToPool()
        {
            if(!IsDeleted)
            {
                GameObjectPool<CircleRangeBoxItem>.Delete(this);
            }            
        }

        public override void SetRangeShape(IItem item)
        {
            base.SetRangeShape(item);
            CircleItem r = item as CircleItem;
            if (r != null)
            {
                var cir = hitBox as CircleCollider2D;
                if(cir != null)
                {
                    cir.offset = Vector3.zero;
                    cir.transform.position = new Vector2(r.offset.x, r.offset.y);
                    cir.transform.rotation = Quaternion.identity;
                    cir.radius = r.radius;
                }
            }
        }

        public override Collider2D InitCollider()
        {
            return gameObject.AddComponent<CircleCollider2D>();
        }
        public override int HitDetect(int unityLayerMask)
        {
            if (value == null || unityLayerMask == 0)
            {
                return 0;
            }
            CircleItem cir = value as CircleItem;
            if (cir == null)
            {
                return 0;
            }
#if UNITY_EDITOR
            var defaultColor = XMLib.DrawUtility.D.color;
            XMLib.DrawUtility.D.color = Color.yellow;
            XMLib.DrawUtility.D.duration = 1 / 60f;
            var m3 = transform.localToWorldMatrix;
            XMLib.DrawUtility.D.DrawCircle(cir.radius, m3);
            XMLib.DrawUtility.D.color = defaultColor;
#endif
            int count = Physics2D.OverlapCircleNonAlloc(transform.position, cir.radius,hitColiderRes, unityLayerMask);
            for(int i=0;i<count;i++)
            {
                hitColiderRes[i].TryGetComponent<RangeBoxItem2D>(out hitRes[i]);
            }
            return count;
        }
    }
}


